#!/usr/bin/python2.4
#
#Copyright 2009 Google Inc.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
#

"""This module defines all the custom exceptions for digial content wizard."""

import constants


class DigitalContentWizardException(Exception):
  """Base class for all content wizrd specific exceptions."""

  def __init__(self, error_message):
    self.message = error_message

  def __str__(self):
    return 'Exception: ' + self.message


class InvalidMetadataException(DigitalContentWizardException):
  """Metadata file provided is not in proper format."""

  def __init__(self, error_message, row_number=0):
    self.line_number = row_number
    DigitalContentWizardException.__init__(self, error_message)

  def __str__(self):
    line_break = constants.HTML_LINE_SEPARATOR
    return line_break.join(
        [DigitalContentWizardException.__str__(self),
         'Error in Metadata at Row No ' + str(self.line_number)])


class InvalidGlobaldataException(DigitalContentWizardException):
  """Global data  file provided is not in proper format."""

  def __init__(self, error_message, row_number=0):
    self.line_number = row_number
    DigitalContentWizardException.__init__(self, error_message)

  def __str__(self):
    line_break = constants.HTML_LINE_SEPARATOR
    return line_break.join(
        [DigitalContentWizardException.__str__(self),
         'Error in Globaldata at Row No ' + str(self.line_number)])


class GlobalMemoryLostException(DigitalContentWizardException):
  """Exception to be raised when global memory objects are lost."""

  def __init__(self, error_message):
    DigitalContentWizardException.__init__(self, error_message)

  def __str__(self):
    line_break = constants.HTML_LINE_SEPARATOR
    return line_break.join(
        [DigitalContentWizardException.__str__(self),
         'This might be due to heavy load. Please try again.'])


class EncryptionException(DigitalContentWizardException):
  """Exception to be raised when decryption function is given invalid value."""

  def __init__(self, message, invalid_id):
    self.invalid_id = str(invalid_id)
    DigitalContentWizardException.__init__(self, message)

  def __str__(self):
    line_break = constants.HTML_LINE_SEPARATOR
    return line_break.join(
        [DigitalContentWizardException.__str__(self),
         'Error while decrypting id ' + self.invalid_id])


class InvalidVersionException(DigitalContentWizardException):
  """Exception when decryption function returns negative version id."""

  def __init__(self, message, invalid_id):
    self.invalid_id = str(invalid_id)
    DigitalContentWizardException.__init__(self, message)

  def __str__(self):
    line_break = constants.HTML_LINE_SEPARATOR
    return line_break.join(
        [DigitalContentWizardException.__str__(self),
         'Invalid version id specified: ' + self.invalid_id])


class DbException(DigitalContentWizardException):
  """Eception to be raised when App Engine Database transaction fails."""

  def __init__(self, message):
    DigitalContentWizardException.__init__(self, message)

  def __str__(self):
    line_break = constants.HTML_LINE_SEPARATOR
    return line_break.join(
        [DigitalContentWizardException.__str__(self),
         'App Engine database error',
         'Please Try Again Later.'])


class InvalidDocumentException(DigitalContentWizardException):
  """Exception to be raised when error during parsing the document page."""

  def __init__(self, message, invalid_id):
    self.invalid_id = str(invalid_id)
    DigitalContentWizardException.__init__(self, message)

  def __str__(self):
    line_break = constants.HTML_LINE_SEPARATOR
    return line_break.join(
        [DigitalContentWizardException.__str__(self),
         'Error while parsing the document with id ' + self.invalid_id])


class InvalidContentTypeException(DigitalContentWizardException):
  """Exception to be raised when invalid content type is provided."""

  def __init__(self, message, row_number):
    self.row_number = row_number
    DigitalContentWizardException.__init__(self, message)

  def __str__(self):
    line_break = constants.HTML_LINE_SEPARATOR
    return line_break.join(
        [DigitalContentWizardException.__str__(self),
         'Invalid content type at row number  ' + str(self.row_number)])
